{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.chdir(\"../\")\n",
    "\n",
    "from pathlib import Path\n",
    "\n",
    "import hydra\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import torch\n",
    "from matplotlib import colormaps as cm\n",
    "\n",
    "cmap = cm.get_cmap(\"viridis\")\n",
    "\n",
    "hydra.core.global_hydra.GlobalHydra.instance().clear()\n",
    "hydra.initialize(version_base=\"1.3\", config_path=\"../configs/\")\n",
    "cfg = hydra.compose(config_name=\"eval.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = Path(\"logs/calibrate/runs\")\n",
    "ckpt_path = root_dir / \"<experiment folder>/cmodel.ckpt\"\n",
    "\n",
    "model = hydra.utils.instantiate(cfg.model)\n",
    "checkpoint = torch.load(ckpt_path, map_location=torch.device(\"cpu\"))\n",
    "model.load_state_dict(checkpoint[\"state_dict\"])\n",
    "nc_curves = checkpoint[\"nc_curves\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cfg.data.batch_size = 6\n",
    "dm = hydra.utils.instantiate(cfg.data)\n",
    "dm.setup(\"test\")\n",
    "\n",
    "dl = dm.test_dataloader()\n",
    "\n",
    "batch = next(iter(dl))\n",
    "\n",
    "print(batch[\"image\"].shape)\n",
    "\n",
    "out = model(batch[\"image\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seg_pprobs = torch.sigmoid(out[\"seg_logits\"])\n",
    "\n",
    "class_idx = 1\n",
    "fg_pprobs = seg_pprobs[:, class_idx]\n",
    "fg_ncs = 1 - fg_pprobs\n",
    "fg_nc_curves = nc_curves[:, class_idx]\n",
    "fg_confs = torch.zeros_like(fg_pprobs)\n",
    "\n",
    "alpha = 0.1\n",
    "mask = fg_ncs <= fg_nc_curves[int((1 - alpha) * 100)]\n",
    "\n",
    "for i in range(batch[\"image\"].shape[0]):\n",
    "    img = batch[\"image\"][i].permute(1, 2, 0).numpy()\n",
    "    gt_mask = batch[\"target_segmentation\"][i][class_idx]\n",
    "    conformal_predicted_mask = mask[i]\n",
    "\n",
    "    fig, ax = plt.subplots(1, 3)\n",
    "    ax[0].imshow(img)\n",
    "\n",
    "    ax[1].imshow(gt_mask, cmap=\"gray\")\n",
    "\n",
    "    ax[2].imshow(conformal_predicted_mask, cmap=\"gray\")\n",
    "\n",
    "    for a in ax:\n",
    "        a.axis(\"off\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "monseg",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
